package com.qzdsoft.erpcloud.controller.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.act.CenterProduct;
import com.qzdsoft.erpcloud.service.act.CenterProductService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.vo.query.act.CenterProductInfo;
import com.qzdsoft.erpcloud.vo.query.act.CenterProductQueryParam;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.act.ActCenterProductPeriodTypeEnum;
import com.qzdsoft.vo.act.ActCenterProductPriceEditEnum;
import com.qzdsoft.vo.act.ActCenterProductTypeEnum;

/**
 * Created by Yang
 */
@Controller
@RequestMapping("/act/center/product")
public class CenterProductController extends BaseController{
    
    private static final Logger logger = LoggerFactory.getLogger(CenterProductController.class);

//    @Autowired
//    private RedisHelper redisHelper;
    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    private CenterProductService actCenterProductService;

    @Permission(validate=false)
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> periodTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> periodTypeMap = new HashMap<Object, Object>();
        for(ActCenterProductPeriodTypeEnum e : ActCenterProductPeriodTypeEnum.values()){
            SysUtils.enums(periodTypeList, periodTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> priceEditList = new ArrayList<EnumPo>();
        Map<Object, Object> priceEditMap = new HashMap<Object, Object>();
        for(ActCenterProductPriceEditEnum e : ActCenterProductPriceEditEnum.values()){
            SysUtils.enums(priceEditList, priceEditMap, e.getKey(), e.getVal());
        }

        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(ActCenterProductTypeEnum e : ActCenterProductTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        mv.addObject("periodTypeList", periodTypeList);
        mv.addObject("periodTypeMap", periodTypeMap);

        mv.addObject("priceEditList", priceEditList);
        mv.addObject("priceEditMap", priceEditMap);

        mv.addObject("typeList", typeList);
        mv.addObject("typeMap", typeMap);

//        mv.addObject("sysRoleMap", redisHelper.getSysRoleMap());
        mv.addObject("sysRoleMap", sysRoleService.getAllRoleList());
        mv.setViewName("act/center_product/center_product");
        return mv;
    }

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer CompanyId,Integer id) {
        
        ModelAndView mv = new ModelAndView();
        List<EnumPo> periodTypeList = new ArrayList<EnumPo>();
        Map<Object, Object> periodTypeMap = new HashMap<Object, Object>();
        for(ActCenterProductPeriodTypeEnum e : ActCenterProductPeriodTypeEnum.values()){
            SysUtils.enums(periodTypeList, periodTypeMap, e.getKey(), e.getVal());
        }

        List<EnumPo> priceEditList = new ArrayList<EnumPo>();
        Map<Object, Object> priceEditMap = new HashMap<Object, Object>();
        for(ActCenterProductPriceEditEnum e : ActCenterProductPriceEditEnum.values()){
            SysUtils.enums(priceEditList, priceEditMap, e.getKey(), e.getVal());
        }

        List<EnumPo> typeList = new ArrayList<EnumPo>();
        Map<Object, Object> typeMap = new HashMap<Object, Object>();
        for(ActCenterProductTypeEnum e : ActCenterProductTypeEnum.values()){
            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
        }

        mv.addObject("periodTypeList", periodTypeList);
        mv.addObject("periodTypeMap", periodTypeMap);

        mv.addObject("priceEditList", priceEditList);
        mv.addObject("priceEditMap", priceEditMap);

        mv.addObject("typeList", typeList);
        mv.addObject("typeMap", typeMap);

//        mv.addObject("sysRoleMap", redisHelper.getSysRoleMap());
        mv.addObject("sysRoleMap", sysRoleService.getAllRoleList());
        
        
        if(null != id){
            mv.addObject("centerProduct", actCenterProductService.selectById(id));
        	
        	mv.setViewName("act/center_product/edit");
        }else {
            mv.setViewName("act/center_product/add");
        }
        return mv;
    }

    @ResponseBody
    @SysLog(description="套餐配置修改保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(CenterProduct actCenterProduct) {
        logger.debug("套餐配置修改保存"+actCenterProduct);
        return actCenterProductService.saveOrUpd(actCenterProduct);
    }
    @ResponseBody
    @SysLog(description="套餐配置新增保存")
    @RequestMapping("/create_execute")
    public Object createExecute(CenterProduct actCenterProduct) {
        logger.debug("套餐配置新增保存"+actCenterProduct);
        return actCenterProductService.saveOrUpd(actCenterProduct);
    }
    
    /**
     * 查询数据列表
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<CenterProductInfo> data(CenterProductQueryParam param) {
        return actCenterProductService.selectData(param.getName(), param.getType(), param.getPriceEdit(), param.getPeriodType(), param.getRoleId(), param.getPage(), param.getLimit());
    }
    /**
     * 根据id删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="套餐配置删除")
    @RequestMapping("/del_execute")
    public Response<String> del(HttpServletRequest request, @RequestParam("id") Integer id) {
        logger.debug("套餐配置删除"+id);
        return actCenterProductService.del(id);
    }
    
    /**
     * 查看明细
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        CenterProduct centerProduct = actCenterProductService.selectById(id);
        mv.addObject("centerProduct", centerProduct);
        mv.setViewName("act/center_product/view");
        return mv;
    }

}
